package com.example.moonshine.redchange.Fragment;



import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.annotation.UiThread;
import android.support.v4.app.Fragment;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.example.moonshine.redchange.Activity.BroadCastManager;
import com.example.moonshine.redchange.Activity.changeDataActivity;


import com.example.moonshine.redchange.Helper.DataDBHepler;
import com.example.moonshine.redchange.R;
import com.example.moonshine.redchange.Util.AlertDialog;
import com.example.moonshine.redchange.Util.SaveParmasUtrl;


/**
 * Created by moonshine on 2018/2/4.
 */

public class FirstlListFragment extends Fragment {
    View view;
    Button btn_bei_gan;
    Button btn_bei_shi;
    Button btn_face_gan;
    Button btn_face_shi;
    Button btn_di_gan;
    Button btn_di_shi;

    TextView tv_statusOne;
    TextView tv_statusTwo; 
    private LocalReceiver mReceiver;
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.change1, container, false);
        initview();
        SaveParmasUtrl saveParmasUtrl = new SaveParmasUtrl();
        String oneTest = saveParmasUtrl.getInfo(getContext(),"text","file");
        setTview(oneTest);
        String twoTest = saveParmasUtrl.getInfo(getContext(),"text1","file");
        setTview(twoTest);
        btn_face_gan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent1 = new Intent();
                    intent1.putExtra("values","6");
                    intent1.putExtra("status",tv_statusTwo.getText().toString().trim());
                    intent1.setClass(getActivity(), changeDataActivity.class);
                    startActivity(intent1);
                }
            });
        btn_face_shi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.putExtra("values","3");
                intent1.putExtra("status",tv_statusTwo.getText().toString().trim());
                intent1.setClass(getActivity(), changeDataActivity.class);
                startActivity(intent1);
            }
        });

        btn_bei_gan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.putExtra("values","5");
                intent1.putExtra("status",tv_statusTwo.getText().toString().trim());
                intent1.setClass(getActivity(), changeDataActivity.class);
                startActivity(intent1);
            }
        });

        btn_bei_shi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.putExtra("values","2");
                intent1.putExtra("status",tv_statusTwo.getText().toString().trim());
                intent1.setClass(getActivity(), changeDataActivity.class);
                startActivity(intent1);
            }
        });

        btn_di_gan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.putExtra("values","4");
                intent1.putExtra("status",tv_statusOne.getText().toString().trim());
                intent1.setClass(getActivity(), changeDataActivity.class);
                startActivity(intent1);
            }
        });

        btn_di_shi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.putExtra("values","1");
                intent1.putExtra("status",tv_statusOne.getText().toString().trim());
                intent1.setClass(getActivity(), changeDataActivity.class);
                startActivity(intent1);
            }
        });
        return view;
    }

    public void initview(){
        btn_bei_gan = view.findViewById(R.id.btn_bei_gan);
        btn_bei_shi = view.findViewById(R.id.btn_bei_shi);
        btn_di_gan = view.findViewById(R.id.btn_di_gan);
        btn_di_shi = view.findViewById(R.id.btn_di_shi);
        btn_face_shi = view.findViewById(R.id.btn_face_shi);
        btn_face_gan = view.findViewById(R.id.btn_face_gan);
        tv_statusOne = view.findViewById(R.id.status);
        tv_statusTwo = view.findViewById(R.id.status1);

    }
    //接收广播
    class LocalReceiver extends BroadcastReceiver {

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onReceive(Context context, Intent intent) {
            //收到广播后的处理
            String text = intent.getStringExtra("text");
            try {
//                if ()
                if (text.equals("111")){
                    showNoProject("一涂左侧厚度过小");
                }else if (text.equals("112")){
                    showNoProject("一涂左侧厚度过大");
                }else if (text.equals("122")){
                    showNoProject("一涂右侧厚度过大");
                }else if (text.equals("121")){
                    showNoProject("一涂右侧厚度过小");
                }else if (text.equals("211")){
                    showNoProject("二涂左侧厚度过小");
                }else if (text.equals("212")){
                    showNoProject("二涂左侧厚度过大");
                }else if (text.equals("222")){
                    showNoProject("二涂右侧厚度过大");
                }else if (text.equals("221")){
                    showNoProject("二涂右侧厚度过小");
                }else {
                    setTview(text);
                }
                Log.i("Websocket","websocket："+text);

            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.i("tag", "传参的数字为:" + text);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //发送广播给service
        Intent intent = new Intent();
        intent.putExtra("status", "fragmentList");
        intent.setAction("service");
        BroadCastManager.getInstance().sendBroadCast(getActivity(), intent);

        IntentFilter filter = new IntentFilter();
        filter.addAction("fragmentList_home");
        mReceiver = new LocalReceiver();
        BroadCastManager.getInstance().registerReceiver(getActivity(), mReceiver, filter);//注册广播接收者
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BroadCastManager.getInstance().unregisterReceiver(getActivity(), mReceiver);
        Log.i("tag","销毁");
    }

    public void setTview(String text) {
        if (text != null) {
            if (text.equals("finishOn")) {
                tv_statusTwo.setText("自动");
            } else if (text.equals("finishOff")) {

                tv_statusTwo.setText("手动");
            } else if (text.equals("bottomOn")) {

                tv_statusOne.setText("自动");
            } else if (text.equals("bottomOff")) {
                tv_statusOne.setText("手动");
            }
        }
    }

    //提示警告
    @SuppressLint("ResourceAsColor")
    public void showNoProject(String msg){
        TextView title = new TextView(getContext());
        title.setText("警告");
        title.setGravity(Gravity.CENTER);
        title.setTextSize(20);
        android.support.v7.app.AlertDialog builder = new android.support.v7.app.AlertDialog.Builder(getContext())
                .setMessage(msg)
                .setCustomTitle(title)
                .create();
        builder.show();
    }

}